package mrHunter.web;

import mrHunter.UserInf;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SessionFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        String[] webFilter=new String[]{"/problemList","/status","tutorial"};
        String url=httpServletRequest.getRequestURI();

        boolean doFilter=false;
        for (String s:webFilter){
            if(url.contains(s)) {
                doFilter = true;
                break;
            }
        }

        if(doFilter){
            Object obj = httpServletRequest.getSession().getAttribute("user");
            if(null==obj) {
                httpServletResponse.sendRedirect("/login");
            }
            else{
                UserInf user=(UserInf)obj;
                filterChain.doFilter(httpServletRequest,httpServletResponse);
            }
        }
        else
            filterChain.doFilter(httpServletRequest,httpServletResponse);
    }
}
